package com.jing.weibo.service.user;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jing.system.model.PageCls;
import com.jing.system.utils.DateUtil;
import com.jing.weibo.mapper.user.UserSignMapper;
import com.jing.weibo.pojo.user.UserPointLog;
import com.jing.weibo.pojo.user.UserSign;

/**
 * 用户签到的Service
 * @author jing.yue
 * @version 2012/10/05 1.0.0
 */
@Component
public class UserSignService {

	@Autowired
	private UserSignMapper userSignMapper;

	/**
	 * 保存用户签到信息
	 * @param userSign
	 * @throws Exception
	 */
	public void saveUserSign(UserSign userSign) throws Exception {
		userSignMapper.saveUserSign(userSign);
	}

	/**
	 * 根据用户名获取用户签到记录信息
	 * @param userSign
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> findByUsernameSign(UserSign userSign) throws Exception {
		List<UserPointLog> userSigns = userSignMapper.findByUsernameSign(userSign);
		Integer countSize = userSignMapper.findByUsernameSignCount(userSign);
		PageCls pageCls = PageCls.setPageCls(userSign.getPage(), userSign.getSize(), null, null, countSize, userSigns.size(), userSigns);
		return pageCls.getPageInfo();
	}

	/**
	 * 查询用户今天的签到对象
	 * @param userSign
	 * @return
	 * @throws Exception
	 */
	public UserSign findByUsernameTodaySign(UserSign userSign) throws Exception {
		userSign.setStringtime(DateUtil.dateToString(DateUtil.getTime(), DateUtil.FMT_YYYY_MM_DD));
		List<UserSign> list = userSignMapper.findByUsernameTodaySign(userSign);
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

}